package are.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class XmlOrders
 */
@WebServlet("/XmlTransformador")
public class XmlTransformador extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public XmlTransformador() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void ProcesarPeticion(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
    	
    	response.setContentType("text/html;charset=ISO-8859-1");
        PrintWriter out = response.getWriter();
        
        out.println("<?xml version=\"1.0\" ?>");
        db conexion = null;
        
        try {
        	
        	conexion = new db();
        	String key = (String)request.getParameter("key");
        	ManejadorTransformador MT = new ManejadorTransformador(conexion);
			out.println("<transformador>");
			if (MT.Find(key)) {
				Transformador trafo = new Transformador();
				trafo = MT.getTrafo();
	            out.println("<item>");
	            out.println("<codigo>" + trafo.getCodigo()  + "</codigo>");
	            out.println("<matricula>" + trafo.getMatricula() + "</matricula>");
	            out.println("<potencia>" + trafo.getPotencia() + "</potencia>");
	            out.println("<fase>" + trafo.getFase()+ "</fase>");
	            out.println("<serie>" + trafo.getSerie() + "</serie>");
	            out.println("<placa>" + trafo.getPlaca_tecnica() + "</placa>");
	            out.println("<valvula>" + trafo.getValvula() + "</valvula>");
	            out.println("<latitud>" + trafo.getLatitud() + "</latitud>");
	            out.println("<longitud>" + trafo.getLongitud() + "</longitud>");
	            
	            out.println("</item>");
			}
            out.println("</transformador>");
            
        	
        	
        } catch (SQLException e) {
			// TODO Auto-generated catch block
			out.print("Error de conexion con el servidor: "
					+ e.getMessage());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			out.print("Error:  "
					+ e.getMessage());
		} finally {
			if (conexion != null) {
				try {
					conexion.Close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					out.print(e.getMessage());
				}
			}
		}
        
    	
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.ProcesarPeticion(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.ProcesarPeticion(request, response);
	}

}
